package UILayer;
import ModelLayer.*;
import CtrlLayer.*;
import java.util.*;

public class ProductsUI
{
    ProductController productController;
    public ProductsUI()
    {
        productController = new ProductController();
    }

    public void productsMenuStart(int sessionId)
    {

        boolean exit=false;
        while (!exit) 
        {
            int choice = writeProductsMenu(sessionId);

            switch(choice){
                case 1:
                startCreateProduct(sessionId);
                break;
                case 2:
                startFindProduct();
                break;
                case 3:
                startUpdateProduct(sessionId);
                break;
                case 4:
                startDeleteProduct();
                break;
                case 0:
                exit = true;
                break;

            }

        }
    }

    private int writeProductsMenu(int sessionId)
    {       

        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f *** Products Menu ***");
        if(SessionContainer.getInstance().findSessionById(sessionId).getAccessLevel() == 2 ){
            System.out.println(" (1) Add product");
            System.out.println(" (2) Show product");
            System.out.println(" (3) Update product");
        }else{
            System.out.println(" (1) Add product(without price, leasing price, min and max amount)");
            System.out.println(" (2) Show product");
            System.out.println(" (3) Update product(without price, leasing price, min andmax amount)");

        }

        if(SessionContainer.getInstance().findSessionById(sessionId).getAccessLevel() == 2 ){
            System.out.println(" (4) Delete a product"); 
        }

        System.out.println(" (0) Back");
        System.out.print("\n Make your choice: ");
        int choice = keyboard.nextInt();
        return choice;
    }

    private void startCreateProduct(int  sessionId)
    {

        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter barcode: ");
        int barcode = Integer.parseInt(keyboard.nextLine());

        System.out.println("Enter name: ");
        String name = keyboard.nextLine();

        double sellingPrice = 0;
        double leasingPrice = 0;
        double priceForDiscount = 0;
        int amountForDiscount = 0;
        int minAmount = 0;
        int maxAmount = 0;
        if(SessionContainer.getInstance().findSessionById(sessionId).getAccessLevel() == 2 ){
            System.out.println("Enter selling price: ");
            sellingPrice = Double.parseDouble(keyboard.nextLine());

            System.out.println("Enter leasing price: ");
            leasingPrice = Double.parseDouble(keyboard.nextLine());

            System.out.println("Enter price for discount: ");
            priceForDiscount = Double.parseDouble(keyboard.nextLine());
            
            System.out.println("Enter amount for discount: ");
            amountForDiscount = Integer.parseInt(keyboard.nextLine());

            System.out.println("Enter minimum amount: ");
            minAmount = Integer.parseInt(keyboard.nextLine());

            System.out.println("Enter maximum amount: ");
            maxAmount = Integer.parseInt(keyboard.nextLine());
        }

        System.out.println("Enter amount: ");
        int amount = Integer.parseInt(keyboard.nextLine());
        
        System.out.println("Enter contractor ID: ");
        int contractorId = Integer.parseInt(keyboard.nextLine());

        System.out.println("Enter location code: ");
        int category =  Integer.parseInt(keyboard.nextLine());

        if(productController.createProduct(barcode, name, sellingPrice, leasingPrice, priceForDiscount,
        amountForDiscount, amount, minAmount, maxAmount, contractorId, category)) {
            System.out.println("Product with barcode " + barcode + " has been added!");
        } else {
            System.out.println("Product with barcode " + barcode + " already exists!");
        }
        pause();
    }

    private void startFindProduct()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter barcode: ");
        int barcode = Integer.parseInt(keyboard.nextLine());

        Product p = productController.findProduct(barcode);

        System.out.println("Barcode: ");
        System.out.println(p.getBarcode());

        System.out.println("Name: ");
        System.out.println(p.getName());

        System.out.println("Selling price: ");
        System.out.println(p.getSellingPrice());

        System.out.println("Leasing price: ");
        System.out.println(p.getLeasingPrice());
        
        System.out.println("Price for discount: ");
        System.out.println(p.getPriceForDiscount());
        
        System.out.println("Amount for discount: ");
        System.out.println(p.getAmountForDiscount());

        System.out.println("Amount: ");
        System.out.println(p.getAmount());

        System.out.println("Minimum amount: ");
        System.out.println(p.getMinAmount());

        System.out.println("Maximum amount: ");
        System.out.println(p.getMaxAmount());
        
        System.out.println("Ordered amount: ");
        System.out.println(p.getOrderAmount());
        
        System.out.println("Contractor: ");
        System.out.println(p.getContractorId());

        System.out.println("Location: ");
        System.out.println("Location code: " + p.getLocationCode() + "; " + p.getLocation());

        pause();
    }

    private void startUpdateProduct(int sessionId){
        Scanner keyboard = new Scanner(System.in);
        
        System.out.println("Input barcode");
        int barcode = Integer.parseInt(keyboard.nextLine());
        
        System.out.println("Enter name: ");
        String name = keyboard.nextLine();
        
        double sellingPrice = 0;
        double leasingPrice = 0;
        double priceForDiscount = 0;
        int amountForDiscount = 0;
        int minAmount = 0;
        int maxAmount = 0;
        if(SessionContainer.getInstance().findSessionById(sessionId).getAccessLevel() == 2 ){
            System.out.println("Enter selling price: ");
            sellingPrice = Double.parseDouble(keyboard.nextLine());

            System.out.println("Enter leasing price: ");
            leasingPrice = Double.parseDouble(keyboard.nextLine());

            System.out.println("Enter price for discount: ");
            priceForDiscount = Double.parseDouble(keyboard.nextLine());
            
            System.out.println("Enter amount for discount: ");
            amountForDiscount = Integer.parseInt(keyboard.nextLine());

            System.out.println("Enter minimum amount: ");
            minAmount = Integer.parseInt(keyboard.nextLine());

            System.out.println("Enter maximum amount: ");
            maxAmount = Integer.parseInt(keyboard.nextLine());
        }
        System.out.println("Enter amount: ");
        int amount = Integer.parseInt(keyboard.nextLine());
        
        System.out.println("Enter contractorId : ");
        int contractorId = Integer.parseInt(keyboard.nextLine());
        
        System.out.println("Enter location code: ");
        int category = Integer.parseInt(keyboard.nextLine());
        
        productController.updateProduct(barcode, name, sellingPrice, leasingPrice, priceForDiscount, 
        amountForDiscount, amount, minAmount, maxAmount, contractorId, category);

    }

    private void startDeleteProduct(){
        try{
            Scanner keyboard = new Scanner(System.in);
            System.out.println("Input barcode:");
            int barcode = Integer.parseInt(keyboard.nextLine());
            productController.deleteProduct(barcode);
        }catch(Exception e){
            System.out.println("Input barcode doesn't exist");
        }
        pause();
    }

    private void pause()
    {
        Scanner keyboard = new Scanner(System.in);
        String vent = keyboard.nextLine();
    }
}
